﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrafficArchives.Common;
using TrafficArchives.DataAccess;
using TrafficArchives.Utilities;

namespace TrafficArchives.Users
{
    public partial class ModifyPassword : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void ValidateOriginalPassword(object source, ServerValidateEventArgs args)
        {
            var pwd = Md5.Encrypt(TxtOriginalPwd.Text);
            var db  = new UserDb();
            var data = db.GetData(LoginUser.Current.UserId);
            if (data.Rows.Count > 0)
            {
                var origialPwd = data.Rows[0]["userpassword"].ToString();
                args.IsValid = origialPwd == pwd;
                TxtOriginalPwdValidator.Visible = args.IsValid;
            }
            else
            {
                args.IsValid = false;
            }
            UpdatePanelForm.Update();
        }

        protected void BtnSaveClick(object sender, EventArgs e)
        {
            var db = new UserDb();
            var data = db.GetData(LoginUser.Current.UserId);
            if (data.Rows.Count > 0)
            {
                data.Rows[0]["userpassword"] = Md5.Encrypt(TxtNewPwd.Text);
                db.Update(data);
                LblSuccess.Visible = true;
            }
        }
    }
}